package icu.makka.yygh.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import icu.makka.yygh.model.user.UserInfo;
import icu.makka.yygh.result.Result;
import icu.makka.yygh.user.service.UserInfoService;
import icu.makka.yygh.user.service.impl.UserInfoServiceImpl;
import icu.makka.yygh.vo.user.UserInfoQueryVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private UserInfoServiceImpl userInfoService;

    /**
     * 用户列表
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/{page}/{limit}")
    public Result list(@PathVariable("page") Long page, @PathVariable("limit") Long limit,
                       UserInfoQueryVo userInfoQueryVo) {
        log.info("用户列表，page={}，limit={}，userInfoQueryVo={}", page, limit, userInfoQueryVo);
        Page<UserInfo> pageParam = new Page<>(page, limit);
        IPage<UserInfo> pageModel = userInfoService.selectPage(pageParam, userInfoQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 用户锁定
     * @param userId
     * @param status
     * @return
     */
    @ApiOperation(value = "用户锁定")
    @GetMapping("/lock/{userId}/{status}")
    public Result lock(@PathVariable("userId") Long userId,
                       @PathVariable("status") Integer status) {
        log.info("用户锁定，userId={}，status={}", userId, status);
        boolean flag = userInfoService.lock(userId, status);
        return flag ? Result.ok() : Result.fail("用户锁定失败!");
    }

    /**
     * 用户详情
     * @param userId
     * @return
     */
    @GetMapping("/show/{userId}")
    public Result show(@PathVariable("userId") Long userId) {
        log.info("根据id查询用户详情，userId={}", userId);
        Map<String, Object> map = userInfoService.show(userId);
        return Result.ok(map);
    }

    /**
     * 认证审批
     * @param userId
     * @param authStatus
     * @return
     */
    @GetMapping("/approval/{userId}/{authStatus}")
    public Result approval(@PathVariable("userId") Long userId, @PathVariable("authStatus") Integer authStatus) {
        log.info("认证审批，userId={}，authStatus={}", userId, authStatus);
        Boolean flag = userInfoService.approval(userId, authStatus);
        return flag ? Result.ok() : Result.fail("认证失败");
    }
}
